package net.sample.gismeteo.gui;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import net.sample.gismeteo.R;
import net.sample.gismeteo.WeatherInfo;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * The adapter for weather forecast data list.
 * @author vkozlov
 * @version $Id: $
 */
public class WeatherDataListAdapter extends BaseAdapter {

	private List<WeatherInfo> _forecast;
	private Activity _context;

	public WeatherDataListAdapter(Activity context, List<WeatherInfo> forecast, ListView listView) {
		_context = context;
		_forecast = forecast;
	}

	public int getCount() {
		return _forecast.size();
	}

	public Object getItem(int position) {
		return _forecast.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public void setSelected(int index) {
		notifyDataSetChanged();
	}

	/**
	 * Creates renderer.
	 * @param position the item position in list
	 * @param convert View the view to convert
	 * @param parent the parent view
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		View cellRendererView = null;
		if (convertView == null) {
			Log.i(getClass().getSimpleName(), "creating a CellRendererView object");
			cellRendererView = _context.getLayoutInflater().inflate(R.layout.weatheritem, null);
		} else {
			cellRendererView = (View) convertView;
		}
		// update the cell renderer, and handle selection state
		updateRenderer(position, cellRendererView);
		
		return cellRendererView;
	}

	/**
	 * Updates data in given renderer view.
	 * @param position the item position in list
	 * @param cellRendererView the renderer view
	 */
	private void updateRenderer(int position, View cellRendererView) {
		TextView dateText = (TextView) cellRendererView.findViewById(R.id.dateText);
		TextView pressureText = (TextView) cellRendererView.findViewById(R.id.pressureText);
		TextView temperatureText = (TextView) cellRendererView.findViewById(R.id.temperatureText);
		TextView windText = (TextView) cellRendererView.findViewById(R.id.windDirText);
		ImageView weatherIcon = (ImageView) cellRendererView.findViewById(R.id.weatherIcon);
		
		WeatherInfo weatherInfo = _forecast.get(position);
		
		pressureText.setText(String.format("Pressure: %s ... %s mm Hg", 
				weatherInfo.getPressureMin(), weatherInfo.getPressureMax()));
		
		temperatureText.setText(String.format("Temperature: %s ... %s C", 
				weatherInfo.getTemperatureMin(), weatherInfo.getTemperatureMax()));
		
		windText.setText(String.format("Wind: %s %s - %s m/sec",
				decodeWind(weatherInfo.getWindDir()),
				weatherInfo.getWindMin(), weatherInfo.getWindMax()));
		
		weatherIcon.setImageDrawable(getDrawable(weatherInfo.getWeatherImageResourceName()));
		
		Calendar cal = Calendar.getInstance();
		cal.set(weatherInfo.getYear(), weatherInfo.getMonth() - 1, weatherInfo.getDay(), 
				weatherInfo.getHour(), 0);
		dateText.setText(new SimpleDateFormat("dd-MMM-yyyy HH:mm").format(cal.getTime()));
	}

	/**
	 * Load drawable from resources by its name, using reflection.
	 * @param name the resource name
	 * @return the drawable resource
	 */
    private Drawable getDrawable(String name) {
        Class<R.drawable> c = R.drawable.class;
        Drawable d = null;
        Field f;
        int i = 0;
 
        try {
            f = c.getField(name);
            i = f.getInt(f);
            d = _context.getResources().getDrawable(i);
        } catch (Exception e) {
            Log.e("TAG", String.format("Can not load drawable [%1$s]", name), e);
        }
        return d;
    }
    
    private String decodeWind(int windDir) {
    	String[] windDirectionNames = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
    	return windDirectionNames[windDir];
    }
	
}

